using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace GraphMyCode.Client.Utils
{
	public class BrushFactory
	{
		private static readonly ColorBrightnessConverter Converter = new ColorBrightnessConverter();
		private static readonly Dictionary<Color, Brush> Lookup = new Dictionary<Color, Brush>();

		public static Brush GetBrush(Color color)
		{
			if (Lookup.ContainsKey(color))
				return Lookup[color];

			Brush b = CreateSolidBrush(color);

			return b;
		}

		private static Brush CreateSolidBrush(Color color)
		{
			color.A = 100;
			return new SolidColorBrush(color);
		}

		private static RadialGradientBrush CreateLighterBrush(Color color)
		{
			var b = new RadialGradientBrush
			        	{
			        		GradientOrigin = new Point(0.3, 0.3),
			        		Center = new Point(0.3, 0.3),
			        		RadiusX = 0.5,
			        		RadiusY = 0.5
			        	};

			b.GradientStops.Add(new GradientStop { Color = Colors.White, Offset = 0 });
			b.GradientStops.Add(new GradientStop { Color = color, Offset = 0.65 });
			b.GradientStops.Add(new GradientStop
			                    	{
			                    		Color = (Color)Converter.Convert(color, typeof(Color), 5, CultureInfo.CurrentCulture),
			                    		Offset = 1
			                    	});
			b.GradientStops.Add(new GradientStop
			                    	{
			                    		Color = (Color)Converter.Convert(color, typeof(Color), 15, CultureInfo.CurrentCulture),
			                    		Offset = 1.3
			                    	});

			Lookup.Add(color, b);
			return b;
		}

		private static RadialGradientBrush CreateDarkerBrush(Color color)
		{
			var b = new RadialGradientBrush
			        	{
			        		GradientOrigin = new Point(0.3, 0.3),
			        		Center = new Point(0.3, 0.3),
			        		RadiusX = 0.5,
			        		RadiusY = 0.5
			        	};

			b.GradientStops.Add(new GradientStop
			                    	{
			                    		Color = (Color)Converter.Convert(color, typeof(Color), 15, CultureInfo.CurrentCulture),
			                    		Offset = 0
			                    	});
			b.GradientStops.Add(new GradientStop { Color = color, Offset = 0.65 });
			b.GradientStops.Add(new GradientStop
			                    	{
			                    		Color = (Color)Converter.Convert(color, typeof(Color), -12, CultureInfo.CurrentCulture),
			                    		Offset = 1
			                    	});
			b.GradientStops.Add(new GradientStop
			                    	{
			                    		Color = (Color)Converter.Convert(color, typeof(Color), -25, CultureInfo.CurrentCulture),
			                    		Offset = 1.3
			                    	});

			Lookup.Add(color, b);
			return b;
		}

		private static RadialGradientBrush CreateOpacityBrush(Color color)
		{
			var b = new RadialGradientBrush
			{
				GradientOrigin = new Point(0.3, 0.3),
				Center = new Point(0.3, 0.3),
				RadiusX = 0.5,
				RadiusY = 0.5
			};
			color.A = 150;
			b.GradientStops.Add(new GradientStop
			{
				Color = color,
				Offset = 0
			});
			color.A = 125;
			b.GradientStops.Add(new GradientStop { Color = color, Offset = 0.65 });

			color.A = 100;
			b.GradientStops.Add(new GradientStop
			{
				Color = color,
				Offset = 1
			});

			color.A = 50;
			b.GradientStops.Add(new GradientStop
			{
				Color = color,
				Offset = 1.3
			});

			color.A = 255;
			Lookup.Add(color, b);
			
			return b;
		}
	}
}